package com.shsnc.agent.util;

import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import cn.hutool.core.util.StrUtil;

/**
 *
 * @author zll
 * @date 2021/8/5
 */
public class TemplateTranslateProcessor {

	private Pattern pattern = Pattern.compile("\\{\\{[^{].*?[^}]}}");


	public String realText(String text, Map<String,String> dataSource) {
		if (!text.contains("{{")) {
			//没有要替换的占位符
			return text;
		}
		Matcher matcher = pattern.matcher(text);
		while (matcher.find()) {
			String content = matcher.group();
			String key=getKey(content);
			text= StrUtil.replace(text, content, getValue(key,dataSource));
		}
		return text;
	}
	private String getValue(String key,Map<String,String> dataSource){
		if(!dataSource.containsKey(key)){
			return "";
		}
		return dataSource.get(key);
	}

	private  String getKey(String content){
		String key=content.replace("{{","");
		key=key.replace("}}","");
		return key;
	}

}
